package com.xps.and.yuntong.Utils;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import com.wenming.library.LogReport;
import com.wenming.library.save.imp.CrashWriter;
import com.wenming.library.util.LogUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;

/**
 * Created by XPS on 2017/9/6.
 */

public class CustomCrashWriter extends MyBaseSaver {
    private String TAG = "CustomCrashWriter";
    public CustomCrashWriter(Context context) {
        super(context);
    }

    public static final String LOG_FILE_NAME_EXCEPTION;
    private static final Thread.UncaughtExceptionHandler sDefaultHandler;


    public void writeText(File logFile, String content) {
        FileOutputStream outputStream = null;
        try {
            //String incontent = content+"\n"+getExtraMessage();
            String incontent = content;
            String e = this.encodeString(incontent);
            Log.e(TAG,"最终写到文本的Log：\n" + incontent);
            outputStream = new FileOutputStream(logFile);
            outputStream.write(e.getBytes("UTF-8"));
        } catch (Exception var13) {
            Log.e("MyBaseSaver", var13.toString());
            var13.printStackTrace();
        } finally {
            if(outputStream != null) {
                try {
                    outputStream.flush();
                    outputStream.close();
                } catch (Exception var12) {
                    var12.printStackTrace();
                }
            }
        }
    }

    public synchronized void writeCrash(final Thread thread, final Throwable ex, final String tag, final String content) {
        this.mThreadPool.execute(new Runnable() {
            public void run() {
                Class var1 = MyBaseSaver.class;
                synchronized(MyBaseSaver.class) {
                    MyBaseSaver.TimeLogFolder = LogReport.getInstance().getROOT() + "Log/" + MyBaseSaver.yyyy_mm_dd.format(new Date(System.currentTimeMillis())) + "/";
                    File logsDir = new File(MyBaseSaver.TimeLogFolder);
                    File crashFile = new File(logsDir, CrashWriter.LOG_FILE_NAME_EXCEPTION);
                    if(!Environment.getExternalStorageState().equals("mounted")) {
                        LogUtil.d("CrashWriter", "SDcard 不可用");
                    } else {
                        if(!logsDir.exists()) {
                            LogUtil.d("CrashWriter", "logsDir.mkdirs() =  +　" + logsDir.mkdirs());
                        }

                        if(!crashFile.exists()) {
                            CustomCrashWriter.this.createFile(crashFile, CustomCrashWriter.this.mContext);
                        }

                        StringBuilder preContent = new StringBuilder(CustomCrashWriter.this.decodeString(com.wenming.library.util.FileUtil.getText(crashFile)));
                        LogUtil.d("CrashWriter", "读取本地的Crash文件，并且解密 = \n" + preContent.toString());
                        preContent.append(MyBaseSaver.formatLogMsg(tag, content)).append("\n");
                        LogUtil.d("CrashWriter", "即将保存的Crash文件内容 = \n" + preContent.toString());
                        CustomCrashWriter.this.writeText(crashFile, preContent.toString());
                        CustomCrashWriter.sDefaultHandler.uncaughtException(thread, ex);
                    }
                }
            }
        });
    }

//    private String getExtraMessage(){
//        Runtime rt=Runtime.getRuntime();
//        long maxMemory=rt.maxMemory();
//
//        StringBuilder sb = new StringBuilder();
//        SharedPreferences sp = JUtils.getSharedPreference();
//        sb.append("userId:"+"\n");
//        sb.append(sp.getString("userId","")+"\n");
//        sb.append("DriverId:"+"\n");
//        sb.append(sp.getString("DriverId","")+"\n");
//        sb.append("driverName:"+"\n");
//        sb.append(sp.getString("driverName","")+"\n");
//        sb.append("NickName:"+"\n");
//        sb.append(sp.getString("driverNickName","")+"\n");
//
//        sb.append("MaxMemory:"+"\n");
//        sb.append(Long.toString(maxMemory/(1024*1024))+"\n");
//
//        sb.append("\n");
//        sb.append("SDK VERSION:"+"\n");
//        sb.append(Build.VERSION.SDK_INT+"\n");
//
//        return sb.toString();
//    }

    static {
        LOG_FILE_NAME_EXCEPTION = "CrashLog" + LOG_CREATE_TIME + ".txt";
        sDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
    }

}
